package com.huazai.lock;

/**
 * jstack jvm 中的进程工具   jps -l  jstack id
 * @author huazai
 * @date 2024/2/2
 */
public class DeadLock {
    static Object o1 = new Object();
    static Object o2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (o1) {
                System.out.println("有锁o1， 试图获o2");
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (o2) {
                    System.out.println("有锁o2");
                }
            }
        }, "a").start();

        new Thread(() -> {
            synchronized (o2) {
                System.out.println("有锁o1， 试图获o2");
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (o1) {
                    System.out.println("有锁o1");
                }
            }
        }, "a").start();
    }
}
